#include "matNone.h"


matNone::matNone()
{
	diffuse			= color(150,150,150);
	refractionCoef	= 1.0;

	shadedSurfBalckenssCoef = 2.0;
	diffuseMapOrColor = false;
	ID = nextMaterialID;
	nextMaterialID++;
}
matNone::matNone(color _diffuse)
{
	diffuse			= _diffuse;
	refractionCoef	= 1.0;

	shadedSurfBalckenssCoef = 2.0;
	diffuseMapOrColor = false;
	ID = nextMaterialID;
	nextMaterialID++;
}
matNone::~matNone()
{

}
void matNone::getMaterialFromXML(std::ifstream &file)
{
	char line[MAX_LENGTH];
	std::string temp;
	std::string nazwaWezla;

	unsigned int i(0);

	while (nazwaWezla!="/material")
	{
		file.getline(line, MAX_LENGTH);
		temp.clear();
		temp.append(line);
		nazwaWezla.clear();

		i=0;
		while(temp[i] == 9 || temp[i] == 32)
			++i;
		if(temp[i] == '<')
		{
			std::string nazwaWezlaKoniec;
			std::string zawartoscWezla;
			++i;
			while(temp[i] != '>')
			{
				nazwaWezla.push_back(temp[i]);
				++i;
			}
			if(nazwaWezla == "name")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/name")
					this->setName(zawartoscWezla.c_str());
				continue;
			}
			else if(nazwaWezla == "name /")
			{
				continue;
			}
			else if(nazwaWezla == "diffuse")
			{
				color tempCol;
				tempCol.getColorFromXML(file);
				this->setDiffuse(tempCol);
				continue;
			}
			else if(nazwaWezla == "texture")
			{
				++i;
				while(temp[i] != '<')
				{
					zawartoscWezla.push_back(temp[i]);
					++i;
				}
				++i;
				while(temp[i] != '>')
				{
					nazwaWezlaKoniec.push_back(temp[i]);
					++i;
				}
				if(nazwaWezlaKoniec == "/texture")
					this->setTexture(zawartoscWezla.c_str());
				continue;
			}
			
			else if(nazwaWezla == "texture /")
			{
				continue;
			}
		}
	}
}
color matNone::getColor(vector3 objNormal, point colisionCoords, vector3 rayDirection, light *Light, bool shade, texcoords texCoords)
{
	if (shade)
	{
		return color(0.0f,0.0f,0.0f,1.0f);
	}
	else
		if (diffuseMapOrColor)
			return diffuseMap->getColor(texCoords);
		else
			return diffuse;
}
bool matNone::reactToPhoton(photon *Photon, vector3 N, point col)
{
	if( Photon->bounces >=0 )
	{
		Photon->bounces--;	
		vector3 randomVector;
		randomVector.randomDirectedVector(1.0,N);
		Photon->setDirection(randomVector);
		Photon->setPosition(col);
		Photon->setIntensity(Photon->getIntensity()*0.5 + diffuse*0.5 );
		return true;

	}
	if( randNumber(100.0) < 10.0 )
	{
		vector3 randomVector;
		randomVector.randomDirectedVector(1.0,N);
		Photon->setDirection(randomVector);
		Photon->setPosition(col);
		Photon->setIntensity(Photon->getIntensity()*0.5 + diffuse*0.5 );
		return true;
	}
	else
	{
		Photon->setPosition(col);
		return false;
	}
}
int matNone::isSpecial(vector3 *result, vector3 N, vector3 V, point col, double otherN, bool isInside)
{
	return 0;
}